package pers.cyril.study.graphql.demo.dao;

import org.springframework.stereotype.Repository;
import pers.cyril.study.graphql.demo.entity.Area;
import pers.cyril.study.graphql.demo.entity.City;
import pers.cyril.study.graphql.demo.entity.Province;
import pers.cyril.study.graphql.demo.service.AddressServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 说点什么吧
 *
 * @author Wang Cyril
 * @date 2024/7/2 22:22
 */
@Repository
public class AddressDaoImpl implements AddressDao {

    List<Province> provinceList = new ArrayList<>();

    List<City> cityList = new ArrayList<>();

    List<Area> areaList = new ArrayList<>();

    public AddressDaoImpl() {
        Province p1 = new Province("1", "辽宁省");
        Province p2 = new Province("2", "吉林省");
        provinceList.add(p1);
        provinceList.add(p2);

        City c1 = new City("3", "大连市", "1");
        City c2 = new City("4", "吉林市", "2");

        cityList.add(c1);
        cityList.add(c2);

        Area a1 = new Area("5", "甘井子区", "3", "1");
        Area a2 = new Area("6", "白城区", "4", "2");

        areaList.add(a1);
        areaList.add(a2);

        c1.setAreas(Arrays.asList(a1));
        c2.setAreas(Arrays.asList(a2));

        p1.setCities(Arrays.asList(c1));
        p2.setCities(Arrays.asList(c2));
    }

    @Override
    public List<Province> getAll() {
        return provinceList;
    }

    @Override
    public Province getProvince(String code) {
        for (Province province : provinceList) {
            if (province.getCode().equals(code)) {
                return province;
            }
        }
        return null;
    }

    @Override
    public City getCity(String code) {
        for (City city : cityList) {
            if (city.getCode().equals(code)) {
                return city;
            }
        }
        return null;
    }

    @Override
    public Area getArea(String code) {
        for (Area area : areaList) {
            if (area.getCode().equals(code)) {
                return area;
            }
        }
        return null;
    }

    @Override
    public List<City> getCitiesByPCode(String pCode) {
        for (Province province : provinceList) {
            if (province.getCode().equals(pCode)) {
                return province.getCities();
            }
        }
        return null;
    }

    @Override
    public List<Area> getAreasByPcode(String pCode) {
        for (City city : cityList) {
            if (city.getCode().equals(pCode)) {
                return city.getAreas();
            }
        }
        return null;
    }
}
